<?php

namespace app\index\controller;

use app\common\model\Feedback;
use think\Validate;
use think\Db;

class Index extends Basecontroller
{
    protected $noNeedLogin = ['index','user_reference','about'];

    /**
     * 个人页
     */
    public function index()
    {

        $user = Db::name('user')->where(array('openid' => session('openid')))->find();

        $this->assign('user',$user);
        return $this->fetch();
    }

    /**
     * 个人资料
     */
    public function user_info()
    {
        if ($this->request->isPost()) {
            $name = $this->request->param("name");
            $sex = $this->request->param("sex");
            $avatar = $this->request->param("avatar");

            $sex = get_sex_num($sex);

            $this->user->allowField(true)->save(['name' => $name, 'sex' => $sex,'avatar'=>$avatar]);
            $this->success("保存成功", url('index/index'));


        }
        return $this->fetch();

    }

    /**
     * 用户指南
     */
    public function user_reference()
    {

        $article_yhzn = $this->getArticle(['type' => 1, 'is_deleted' => 1]);
        $this->assign('article_yhzn', $article_yhzn);
        return $this->fetch();
    }

    /**
     * 关于我们
     */
    public function about()
    {
        $article_gywm = $this->getArticle(['type' => 2, 'is_deleted' => 1]);
        $this->assign('article_gywm', $article_gywm);
        return $this->fetch();

    }

    /**
     * 用户反馈
     */
    public function feedback()
    {
        if ($this->request->isPost()) {
            $content = $this->request->param("content");
            $contact = $this->request->param("contact");
            $rule = [
                'content' => 'require'
            ];
            $msg = [
                'content.require' => '建议内容必填',
            ];
            $data = [
                'content' => $content,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError());

            }

            Feedback::create(['content' => $content, 'contact' => $contact,'userid'=>session('yxuid')]);
            $this->success("反馈成功", url('index/index'));

        }
        return $this->fetch();

    }

    /**
     *
     * 重置密码
     */
    public function reset_pwd()
    {

        if ($this->request->isPost()) {
            $password = $this->request->param("password");
            $phone = $this->request->param("phone");
            $password_confirm = $this->request->param("password_confirm");
          //  $msg_code = $this->request->param("msg_code");

            $rule = [
                'phone' => 'require',
//                'msg_code' =>'require',

                'password' => 'require|confirm:password_confirm'
            ];
            $msg = [
                'phone.require' => '手机号必填',
                'password.require' => '密码必填',
                'password.confirm' => '两次密码不一致',
             //   'msg_code' =>'短信验证码',

            ];
            $data = [
                'phone' => $phone,
                'password' => $password,
                'password_confirm' => $password_confirm,
               // 'msg_code' =>$msg_code,


            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError());

            }
/*
            if($msg_code != session('msg_code')){
                $this->error("验证码不正确");
            }*/

            $this->user->allowField(true)->save(['password' => md5($password)]);
            $this->user->logout();
            $this->success("重置成功", url('login/index'));


        }



        return $this->fetch();

    }


}
